package MainPackage;


import java.awt.*;
import java.util.Calendar;
import java.util.Date;
import javax.swing.*;
import javax.swing.border.*;

public class InfoBookFrame extends JFrame
{
	private final int w = 1000;
	private final int h = 700;
	private final Toolkit kit = Toolkit.getDefaultToolkit();
	private final Dimension screenSize = kit.getScreenSize();
	private final Dimension windowSize = new Dimension(w, h);
	
	public InfoBookFrame(String s)
	{
		setSize(windowSize.width, windowSize.height);
		setLocation((screenSize.width/2)-(windowSize.width/2), (screenSize.height/2)-(windowSize.height/2));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle(s);
		
		drawLayout();
		
		setVisible(true);
	}
	
	private void drawLayout()
	{
		setLayout(new BorderLayout());

		JPanel infoPanel = new JPanel();
		
		JPanel basicInfo = new JPanel();
		Border biPanelBorder = BorderFactory.createEtchedBorder();
		biPanelBorder = BorderFactory.createTitledBorder(biPanelBorder, "information");
		basicInfo.setBorder(biPanelBorder);
		biPanelBorder = BorderFactory.createTitledBorder(biPanelBorder, "information");
		basicInfo.setBorder(biPanelBorder);
		basicInfo.setLayout(new GridLayout(7, 2, 10, 10));//row column hgap vgap
		DatePanel regPan = new DatePanel();
		DatePanel payDayPan = new DatePanel();
		JTextField nameField = new JTextField();
		JTextField phonenumField = new JTextField();
		JTextField moneyField = new JTextField();
		JComboBox classBox = new JComboBox();
		classBox.addItem(" ");
		classBox.addItem("세잔");
		classBox.addItem("고흐");
		classBox.addItem("모네");
		JComboBox paymentBox = new JComboBox();
		paymentBox.addItem(" ");
		paymentBox.addItem("카드");
		paymentBox.addItem("현금");
		basicInfo.add(new JLabel("name")); basicInfo.add(nameField);
		basicInfo.add(new JLabel("phone number")); basicInfo.add(phonenumField);
		basicInfo.add(new JLabel("registered date")); basicInfo.add(regPan.getPanel());
		basicInfo.add(new JLabel("payment date")); basicInfo.add(payDayPan.getPanel());
		basicInfo.add(new JLabel("money")); basicInfo.add(moneyField);
		basicInfo.add(new JLabel("class name")); basicInfo.add(classBox);
		basicInfo.add(new JLabel("payment")); basicInfo.add(paymentBox);
		
		
		JPanel additionalInfo = new JPanel();
		Dimension infoSize = new Dimension((int)w/3, (int)h/3);
		Border aiPanelBorder = BorderFactory.createEtchedBorder();
		aiPanelBorder = BorderFactory.createTitledBorder(aiPanelBorder, "more..");
		additionalInfo.setPreferredSize(infoSize);
		additionalInfo.setBorder(aiPanelBorder);
		additionalInfo.setLayout(new BorderLayout());
		JTextArea textarea = new JTextArea();
		additionalInfo.add(textarea);
		
		JPanel controlButtons = new JPanel();
		JButton newButton = new JButton("new");
		JButton saveButton = new JButton("save");
		JButton searchButton = new JButton("search");
		JButton detailsButton = new JButton("details");
		JButton payButton = new JButton("pay");
		controlButtons.add(newButton);
		controlButtons.add(saveButton);
		controlButtons.add(searchButton);
		controlButtons.add(detailsButton);
		controlButtons.add(payButton);
		newButton.addActionListener(new NewButtonEventHandler(nameField, phonenumField, regPan, payDayPan, moneyField, classBox, paymentBox));
		saveButton.addActionListener(new SaveButtonEventHandler(nameField, phonenumField, regPan, payDayPan, moneyField, classBox, paymentBox));
		searchButton.addActionListener(new SearchButtonEventHandler(nameField, phonenumField, regPan, payDayPan, moneyField, classBox, paymentBox));
		detailsButton.addActionListener(new DetailsButtonEventHandler(nameField, phonenumField, regPan, payDayPan, moneyField, classBox, paymentBox));
		payButton.addActionListener(new PayButtonEventHandler(nameField, phonenumField, regPan, payDayPan, moneyField, classBox, paymentBox));
		
		infoPanel.setLayout(new BorderLayout());
		infoPanel.add(basicInfo, BorderLayout.NORTH);
		infoPanel.add(additionalInfo, BorderLayout.CENTER);
		infoPanel.add(controlButtons, BorderLayout.SOUTH);
		
		
		Dimension tableSize = new Dimension((int)(0.6*(double)w), h);
		JScrollPane tablePanel = new JScrollPane(getStudentTable());
		tablePanel.setPreferredSize(tableSize);
		
		add(infoPanel, BorderLayout.CENTER);
		add(tablePanel, BorderLayout.EAST);
		
		kit.beep();
	}
	
	private JTable getStudentTable()
	{
		String[] names = {"name", "phone number", "registered date", "money", "class", "payment"};
		String[][] contents = { };
		
		return new JTable(contents, names);
	}
}

class DatePanel extends JPanel
{
	private final JPanel p;
	private JTextField yearField;
	private JTextField monthField;
	private JTextField dayField;
	
	public DatePanel()
	{
		p = new JPanel();
		yearField = new JTextField(4); p.add(yearField);
		p.add(new JLabel("y"));
		monthField = new JTextField(2); p.add(monthField);
		p.add(new JLabel("m"));
		dayField = new JTextField(2); p.add(dayField);
		p.add(new JLabel("d"));
	}
	
	public void clearField()
	{
		yearField.setText(" ");
		monthField.setText(" ");
		dayField.setText(" ");
	}
	
	public Date getDate()
	{
		Calendar cal = Calendar.getInstance();
		Integer yearData = new Integer(yearField.getText());
		Integer monthData = new Integer(monthField.getText());
		Integer dayData = new Integer(dayField.getText());
		
		cal.set(Calendar.DAY_OF_YEAR, yearData);
		cal.set(Calendar.MONTH, monthData);
		cal.set(Calendar.DAY_OF_MONTH, dayData);
		
		Date date = cal.getTime();
		return date;
	}
	
	public void setField(String y, String m, String d)
	{
		yearField.setText(y);
		monthField.setText(m);
		dayField.setText(d);
	}
	
	public JPanel getPanel()
	{
		return p;
	}
}